package com.auerdi.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.AnimationUtils.AnimationUtils;
import com.auerdi.R;
import com.auerdi.ui.order.ADOrderFragment;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.ItemList;
import com.example.egghttp.retrofit.beans.banner.ProdictCutDetailContainer;
import com.example.egghttp.retrofit.beans.banner.SpecGoodsList;
import com.example.egghttp.retrofit.beans.banner.SpecItemList;
import com.example.egghttp.retrofit.beans.goods.Product;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by highsixty on 2018/3/15.
 * mail  gaolulin@sunmi.com
 */

public class CheckPinDialog extends AppCompatDialogFragment {


    Unbinder unbinder;
    private static Product mProduct;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.tt)
    View tt;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.ept)
    RelativeLayout ept;
    @BindView(R.id.close)
    TextView close;
    @BindView(R.id.plusleft)
    TextView plusleft;
    @BindView(R.id.minusright)
    TextView minusright;
    @BindView(R.id.btn_ok)
    TextView btn_ok;

    private String mBuyType;
    ArrayList<ItemList> mSpecItemList = new ArrayList<>();
    private List<SpecGoodsList> specGoods;


    public static CheckPinDialog newInstance(Product product, String buyType) {
        mProduct = product;
        CheckPinDialog fragment = new CheckPinDialog();
        Bundle args = new Bundle();
        args.putString("buyType", buyType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        window.setAttributes(windowParams);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBuyType = (String) getArguments().get("buyType");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_check_pin, container, false);
        L.d("hcia", "view:" + view);
        unbinder = ButterKnife.bind(this, view);
        AnimationUtils.slideToUp(view);
        ept.setOnClickListener(v -> {
        });

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.d("hcia", "mBuyType:" + mBuyType);
                AnimationUtils.slideToDown(view, new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                        dismiss();
                        mProduct.setGood_num(num.getText() + "");
                        mProduct.setBuyMode(mBuyType);
                        EventBus.getDefault().post(ADOrderFragment.newInstance(mProduct));
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

            }
        });
        plusleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String ss = (Integer.parseInt(num.getText() + "") + 1) + "";
                num.setText(ss);
            }
        });
        minusright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ss = (Integer.parseInt(num.getText() + "") - 1) + "";
                if (ss.equals("0"))
                    return;

                num.setText(ss);
            }
        });


        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        L.d("hcia", "mBuyType:" + mBuyType);

        container.setOnClickListener(v -> AnimationUtils.slideToDown(view, new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        }));
//        mBuyType
        Picasso.get().load(mProduct.getCover_img_url()).error(R.mipmap.logo).fit().placeholder(R.mipmap.logo).into(image);

        RetrofitClient.getClient().create(AdService.class).collageSkuListLikePin(mProduct.getId())
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe
                        (

                                new Subscriber<BOb<ProdictCutDetailContainer>>() {
                                    @Override
                                    public void onCompleted() {
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        L.d("hcia", "e:" + e);
                                    }

                                    @Override
                                    public void onNext(BOb<ProdictCutDetailContainer> prodictCutDetailContainerBOb) {

                                        specGoods = prodictCutDetailContainerBOb.getBody().getSpecGoodsList();
                                        mProduct.setGood_spec(specGoods.get(0).getSpec_key());

                                        L.d("hcia", "mProduct.getGood_spec():" + mProduct.getGood_spec());

                                        List<SpecItemList> SpecItems = prodictCutDetailContainerBOb.getBody().getSpecItemList();

                                        for (SpecItemList specItemList : SpecItems) {
                                            mSpecItemList.add(specItemList.getItem_list().get(0));
                                        }


                                        Picasso.get().load(specGoods.get(0).getPic()).error(R.mipmap.epta).fit().placeholder(R.mipmap.epta).into(image);


                                    }
                                }
                        );


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
